package com.odm.communitymanage.ui.mine.MyComplaint

import android.content.Intent
import android.net.Network
import android.net.Uri
import android.view.Display
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.UriUtils
import com.bumptech.glide.Glide
import com.odm.communitymanage.R
import com.odm.communitymanage.util.Utils
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.NetCallBack
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.util.GlideUtils
import com.rxjava.rxlife.lifeOnMain
import com.zhihu.matisse.Matisse
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_post_complaint.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 提交 投诉 页面
 * @author: ODM
 * @date: 2020/4/16
 */
class PostComplaintFragment : BaseFragment() {

    var disposable : Disposable ?= null

    var photoUris : MutableList<Uri> = mutableListOf()

    override fun onDestroyView() {
        super.onDestroyView()
        //离开页面 销毁请求
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }

    override fun initViews() {
        btnPostComplaintPhoto.setOnClickListener {
            //跳转页面选择图片，当选择了图片后，依次让iv1,iv2,iv3 设为Visible，iv3也可见后将添加图片按钮设为inVisible！
            Utils.openMatissePage(this)
        }

        //第一次点击选择，第二次点击取消选择
        rlComplaintNoName?.setOnClickListener {
            val state = rbNoNameComplaint.isChecked
            rbNoNameComplaint.isChecked = ! state
        }
        //防止点击事件的冲突
        rbNoNameComplaint.isClickable  = false

        //提交按钮的点击事件
        btnPostComplaint.setOnClickListener {
            postMyComplaintInfo(
                etContent_PostComplaint.text.toString(),
                rbNoNameComplaint?.isChecked ?:false ,
                photoUris)
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //加载从 图库中选择的图片，每次为多张或单张，最多放入3张
        if (requestCode == Constants.REQUEST_IMAGE_CODE && data != null) {
            val result = Matisse.obtainResult(data)
            val uriList: MutableList<Uri> ?= result
            uriList?.let {
                for (uri in uriList) {
                    var target: ImageView ?= null
                    //按顺序显示1 2 3 ，可以控制逐个显示和相对位置
                    if (ivComplaintPhoto_1.visibility != View.VISIBLE) {
                        target = ivComplaintPhoto_1
                    } else if (ivComplaintPhoto_2.visibility != View.VISIBLE) {
                        target = ivComplaintPhoto_2
                    } else if (ivPostComplaintPhoto_3.visibility != View.VISIBLE) {
                        target = ivPostComplaintPhoto_3
                        btnPostComplaintPhoto.visibility = View.INVISIBLE
                    }
                    target?.let {
                        GlideUtils.getInstance()
                            .loadImageUri(requireContext(), uri, target)
                        target.visibility = View.VISIBLE
                        //添加进上传图片的Uri列表
                        photoUris.add(uri)

                    }
                }
            }
        }
    }
    
    
    

    override fun getLayoutId(): Int {
        return R.layout.fragment_post_complaint
    }


    private fun postMyComplaintInfo(content: String,
                                    anonymous: Boolean,
                                    photos:MutableList<Uri>){
        if(content == ""){
            ToastUtils.showShort("投诉内容不可以为空")
            return
        }
        if(photos?.isEmpty() == true){
            ToastUtils.showShort("请选择合适的图片")
            return
        }
        val param = RxHttp.postForm(ApiService.POST_MY_COMPLAINT )
            .add("content",content)
            .add("anonymous",anonymous)
        for(uri in photos){
            val file = Utils.getPhotoFileByUri(uri , true)
            param.add("file" , file)
        }
        disposable = param.asClass(NetCallBack::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code == 1){
                    showSuccessSnack(getString(R.string.post_success))
                    pop()
                }else {
                    ToastUtils.showShort(it.message)
                }
            },{
                ToastUtils.showShort(it.message)
                it.printStackTrace()
            })




    }
}